*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% APPENDIX
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

		*------------------------------------
		* SAVE IN DIFFERENT PATH
		*------------------------------------
		
	global graphs "../Graphs/APPENDIX"
	global tables "../Tables/APPENDIX"
		
		
		*------------------------------------
		* PROJECT-LEVEL ANALYSIS
		*------------------------------------
			
			cap prog drop loaddata_proj
			program loaddata_proj, eclass
				qui {
				*Keep districts used in analysis (have MR data)
				use "$data/analysis_district_clean_all", clear
				keep ubigeo ubigeo_id year pscore s0 s1 s2 s0_max s2_year
				tempfile districts
				save `districts'

				use "$data/projects", clear
				merge m:1 ubigeo year using `districts'
				keep if _merge==3
				unique ubigeo 
				drop _merge

				unique ubigeo snip
				
				*Generate time until start at the project level
				gen p_sewerage_ts0=p_sewerage_y0y_max-2005
				
				}
			end
			

		
			

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% A. Additional material for Section I, Background 
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure A1: Project characteristics
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*Keep districts used in analysis (have MR data)
loaddata_proj

*Type
collapse (max) p_type_cat p_formu p_financ, by(snip ubigeo)

label values p_type_cat p_type_cat

replace p_formu=1 if p_formu==2
replace p_formu=2 if p_formu==3
replace p_formu=3 if p_formu>=4

label define p_level 1 "Local" 2 "Regional" 3 "Central"
label values p_formu p_level

replace p_financ=1 if p_financ==3 | p_financ>=4
label define p_financ2 1 "District" 2 "Central"
label values p_financ p_financ2

tab p_type_cat, gen(p_type_cat_)
tab p_formu, gen(p_formu_)
tab p_financ, gen(p_financ_)

*Formulation
graph bar p_formu_1 p_formu_2 p_formu_3 , percent graphregion(color(white)) title("") ytitle("% projects" ) ylabel(,angle(horizontal)) legend(order(1 "Local" 2 "Regional" 3 "Central") col(3)) bar(1, color(navy)) bar(2, color(gs4)) bar(3, color(gs10))
graph export "$graphs/p_formu.pdf", replace

*Finance
graph bar p_financ_1 p_financ_2  , percent graphregion(color(white)) title("") ytitle("% projects" ) ylabel(,angle(horizontal)) legend(order(1 "Local" 2 "Central") col(2)) bar(1, color(navy)) bar(2, color(gs4))
graph export "$graphs/p_financ.pdf", replace

collapse (mean) p_type_cat_* p_formu_* p_financ_*, by(ubigeo)

*Formulation
graph bar p_formu_1 p_formu_2 p_formu_3 , percent graphregion(color(white)) title("") ytitle("% projects in average district" ) ylabel(,angle(horizontal)) legend(order(1 "Local" 2 "Regional" 3 "Central") col(3)) bar(1, color(navy)) bar(2, color(gs4)) bar(3, color(gs10))
graph export "$graphs/dis_formu.pdf", replace

*Finance
graph bar p_financ_1 p_financ_2  , percent graphregion(color(white)) title("") ytitle("% projects in average district" ) ylabel(,angle(horizontal)) legend(order(1 "Local" 2 "Central") col(2)) bar(1, color(navy)) bar(2, color(gs4))
graph export "$graphs/dis_financ.pdf", replace 


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% B. Additional material for Section II, Data
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure B1: Mortality rates across data sources
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*OTHER DATA (UNIGME 2018 DATA)
*----------------------------------------------
local mr "imr1y u5mr"
foreach y of local mr {
use "$data/APPENDIX/UN_`y'.dta", clear
keep observationvalue seriesname series_name year
rename observationvalue vs_`y'

*Keep only survey data - VR below
drop if regexm(seriesname, "March")

tempfile vs_`y'
save `vs_`y''
}

use `vs_imr1y', clear
merge 1:1 year seriesname using `vs_u5mr', nogen

tempfile mr
save `mr'


*MINSA VITAL RECORDS
*----------------------------------------------
*Keep districts used in analysis (have MR data)
use "$data/analysis_district_clean", clear
keep ubigeo year mort_less1y mort_u5y pop_u1y pop_u5y
tempfile vitalstats
save `vitalstats'

*MINSA births
use `vitalstats', clear
collapse (sum) mort_less1y mort_u5y, by(year)
sort year
merge 1:1 year using "$data/APPENDIX/MINSA_births.dta", nogen

gen vs_imr1y= mort_less1y/vs_births
replace vs_imr1y=vs_imr1y*$mr_per

gen vs_u5mr=mort_u5y/vs_births
replace vs_u5mr=vs_u5mr*$mr_per

append using `mr'

replace seriesname="VS MINSA-births" if seriesname==""

tempfile mr2
save `mr2'


*MINSA POP- WEIGHTED
use `vitalstats', clear
collapse (sum) mort_less1y mort_u5y pop_u1y, by(year)

gen vs_imr1y= mort_less1y/pop_u1y
replace vs_imr1y=vs_imr1y*$mr_per

gen vs_u5mr= mort_u5y/pop_u1y
replace vs_u5mr=vs_u5mr*$mr_per
append using `mr2'

replace seriesname="VS MINSA-pop (weight)" if seriesname==""

tempfile mr3
save `mr3'


*MINSA POP
use `vitalstats', clear
collapse (sum) mort_less1y mort_u5y pop_u5y, by(year)

*IMR approx live births with under-five children population / 5
gen vs_imr1y= mort_less1y/(pop_u5y/5)
replace vs_imr1y= vs_imr1y*$mr_per

gen vs_u5mr=mort_u5y/(pop_u5y/5)
replace vs_u5mr= vs_u5mr*$mr_per

append using `mr3'
replace seriesname="VS MINSA-pop" if seriesname==""

keep if year>=2005 & year<=2015

drop if regexm(seriesname, "UN IGME")

sort year

tempfile mrUN
save `mrUN'


*OTHER SURVEYS (UNIGME 2021 DATA)
*----------------------------------------------
local mr "imr1y u5mr"
foreach y of local mr {
use "$data/APPENDIX/UN_OTHER_`y'.dta", clear
keep OBS_VALUEObservationValue SERIES_NAMESeriesName REF_DATEReferenceDate
rename SERIES_NAMESeriesName seriesname
rename REF_DATEReferenceDate year

replace year=year-0.5

rename OBS_VALUEObservationValue vs_`y'

tempfile vs_`y'
save `vs_`y'', replace
}

use `vs_imr1y', clear
merge 1:1 year seriesname using `vs_u5mr', nogen

tempfile mr4
save `mr4'

*CENSUS 2017 (UNIGME DATA)
*----------------------------------------------

local mr "imr1y u5mr"
foreach y of local mr {
use "$data/APPENDIX/UN_CENSO_`y'.dta", clear
keep OBS_VALUEObservationValue SERIES_NAMESeriesName REF_DATEReferenceDate
rename SERIES_NAMESeriesName seriesname
rename REF_DATEReferenceDate year

replace year=2006 if year==2006.6
replace year=2009 if year==2009.3
replace year=2011 if year==2011.6
replace year=2013 if year==2013.7
replace year=2015 if year==2015.4

rename OBS_VALUEObservationValue vs_`y'

tempfile vs_`y'
save `vs_`y'', replace
}

use `vs_imr1y', clear
merge 1:1 year seriesname using `vs_u5mr', nogen


tempfile mr5
save `mr5'


use  `mrUN', clear
append using `mr4'

replace seriesname="National Demographic and Health Survey 2014 (Direct)" if regexm(seriesname, "1046")
replace seriesname="Demographic and Health Survey 2019" if regexm(seriesname, "1041")
replace seriesname="ENDES2020" if regexm(seriesname, "1885")
replace seriesname="ENDES2021" if regexm(seriesname, "2015: Encuesta")
replace seriesname="ENDES2018 - Direct" if regexm(seriesname, "2017: Encuesta")
replace seriesname="ENDES2017 - Direct " if regexm(seriesname, "2018: Encuesta")
replace seriesname="Demographic and Health Survey 2017" if regexm(seriesname, "Demographic and Family Health Survey 2017")

drop if regexm(seriesname, "2016: Encuesta") //Remove ENDES when computing MR with HH deaths

append using `mr5'

replace seriesname="P Census 2017" if regexm(seriesname, "Census")

encode seriesname, gen(source)

keep if year>=2005 & year<=2015

tempfile mrpoints
save `mrpoints'


*UNIGME ESTIMATES 2021
local mr "imr1y u5mr"
foreach y of local mr {
use "$data/APPENDIX/UN_UNIGME_`y'.dta", clear
 
rename REF_DATEReferenceDate year
rename SERIES_NAMESeriesName seriesname

replace year=year-0.5

rename OBS_VALUEObservationValue vs_`y'
rename LOWER_BOUNDLowerBound vs_`y'_lb
rename UPPER_BOUNDUpperBound vs_`y'_ub

keep year vs_* 

keep if year>=2005 & year<=2015

tempfile vs_`y'
save `vs_`y''
}

use `vs_imr1y', clear
merge 1:1 year using `vs_u5mr'

tempfile mrunigme
save `mrunigme'

use `mrpoints'
append using `mrunigme', force

replace source=21 if source==.

*PLOTS - BY EACH SOURCE
local mr "imr1y"
foreach l of local mr {	
twoway rarea vs_`l'_lb vs_`l'_ub year if source==21, sort fcolor(gs14) lcolor(gs14)  legend(label(1 "UNIGME21")) || line vs_`l' year if source==21, legend(label(2 "UNIGME21")) lcolor(black) || ///
scatter vs_`l' year if source==1, legend(label(3 "DHS09") col(4)) msymbol(o) || scatter vs_`l' year if source==2, legend(label(4 "DHS10") col(4)) msymbol(o) || scatter vs_`l' year if source==3, legend(label(5 "DHS11") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==5, legend(label(6 "DHS13") col(4)) msymbol(o) || scatter vs_`l' year if source==6, legend(label(7 "DHS17") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==7, legend(label(8 "DHS19") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==8, legend(label(9 "ENDES17") col(4)) msymbol(o) || scatter vs_`l' year if source==9, legend(label(10 "ENDES18") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==10, legend(label(11 "ENDES20") col(4)) msymbol(o) || scatter vs_`l' year if source==11, legend(label(12 "ENDES21") col(4)) msymbol(o)  || ///
scatter vs_`l' year if source==13, legend(label(13 "ENDES10") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==14, legend(label(14 "ENDES11") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==15, legend(label(15 "DHS14") col(4)) msymbol(o) mcolor(gs10) || ///
scatter vs_`l' year if source==16, legend(label(16 "Census17") col(4)) mcolor(black) msymbol(Th) || ///
scatter vs_`l' year if source==17, legend(label(17 "VR WHO/UNIGME18") col(4)) mcolor(black) msymbol(Oh) || ///
scatter vs_`l' year if source==18, legend(label(18 "VR births") col(4)) mcolor(black) msymbol(Sh)  || ///
scatter vs_`l' year if source==19, legend(label(19 "VR pop") col(4))  mcolor(black) msymbol(Dh) ///
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none)) xlabel(2005(1)2015) ylabel(0(5)25) ytitle("Infant mortality rate (per 1,000 live births)") xtitle("")  ylabel(0(5)30)
graph export "$graphs/data_`l'.pdf", replace
}

local mr "u5mr"
foreach l of local mr {	
twoway rarea vs_`l'_lb vs_`l'_ub year if source==21, sort fcolor(gs14) lcolor(gs14)  legend(label(1 "UNIGME21")) || line vs_`l' year if source==21, legend(label(2 "UNIGME21")) lcolor(black) || ///
scatter vs_`l' year if source==1, legend(label(3 "DHS09") col(4)) msymbol(o) || scatter vs_`l' year if source==2, legend(label(4 "DHS10") col(4)) msymbol(o) || scatter vs_`l' year if source==3, legend(label(5 "DHS11") col(4)) msymbol(o) || scatter vs_`l' year if source==4, legend(label(6 "DHS12") col(4)) msymbol(o) || scatter vs_`l' year if source==5, legend(label(7 "DHS13") col(4)) msymbol(o) || scatter vs_`l' year if source==6, legend(label(8 "DHS17") col(4)) msymbol(o) || scatter vs_`l' year if source==7, legend(label(9 "DHS19") col(4)) msymbol(o) || ///
scatter vs_`l' year if source==8, legend(label(10 "ENDES17") col(4)) msymbol(o) || scatter vs_`l' year if source==9, legend(label(11 "ENDES18" ) col(4)) msymbol(o) || ///
scatter vs_`l' year if source==10, legend(label(12 "ENDES20") col(4)) msymbol(o) || scatter vs_`l' year if source==11, legend(label(13 "ENDES21" ) col(4)) msymbol(o)  || ///
scatter vs_`l' year if source==15, legend(label(14 "DHS14") col(4)) msymbol(o) mcolor(gs10) || ///
scatter vs_`l' year if source==16, legend(label(15 "Census17") col(4)) mcolor(black) msymbol(Th) || ///
scatter vs_`l' year if source==17, legend(label(16 "VR WHO/UNIGME18") col(4)) mcolor(black) msymbol(Oh) || ///
scatter vs_`l' year if source==18, legend(label(17 "VR births") col(4)) mcolor(black) msymbol(Sh)  || ///
scatter vs_`l' year if source==19, legend(label(18 "VR pop") col(4))  mcolor(black) msymbol(Dh) ///
graphregion(fcolor(white) lcolor(none) ifcolor(none) ilcolor(none)) xlabel(2005(1)2015) ylabel(0(5)25) ytitle("Under-five mortality rate (per 1,000 live births)") xtitle("")  ylabel(0(5)30)
graph export "$graphs/data_`l'.pdf", replace
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table B4: Balance in initial characteristics (2005), by mortality data status
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean", clear

unique ubigeo

cap drop vs_imr1y_max vs_u5mr_max rounds*
keep if year>=2005 & year<=2015
bysort ubigeo: egen vs_imr1y_max=max(vs_imr1y)
bysort ubigeo: egen vs_u5mr_max=max(vs_u5mr)
bysort ubigeo: egen rounds=count(vs_u5mr) 
bysort ubigeo: egen rounds2=count(vs_imr1y)

//Districts with mortality data
gen vs_missno=0 if (vs_imr1y_max==. | vs_u5mr_max==.)
replace vs_missno=1 if vs_missno==.

unique ubigeo if  vs_missno==0 
unique ubigeo if  vs_missno==1 

bysort ubigeo: egen vs_missno_max=max(vs_missno) //max always missing MR data

//Districts with mortality data (at least 2 rounds) + geo data + project data 
gen in_sample=1 if (vs_imr1y_max!=. | vs_u5mr_max!=.) & s0!=. & geo_slope1_perc!=. & rounds>=2 & rounds!=. & rounds2>=2 & rounds2!=.   
replace in_sample=0 if in_sample==.

//In analysis vs. not in analysis
unique ubigeo if  in_sample==0 
unique ubigeo if  in_sample==1

//Globals			
global censo "inei_pop geo_pop_dens05 dis_piped dis_hh_sewer dis_hh_onsite dis_hh_od dis_educ_secp dis_elecp"
global muni "renamu_pia renamu_int renamu_atform renamu_dhealth" 
global muni_prechgm "inei_pop_prechgm geo_pop_dens_prechgm renamu_pia_prechgm renamu_int_prechgm renamu_atform_prechgm renamu_dhealth_prechgm"

global pol "pct_reelection_1993"
global pol_prechgm "turnover_prechgm"

//New variables
lab var inei_pop "\textit{2. District characteristics} \\ Population"
lab var dis_piped "Share HH piped water"
lab var geo_pop_dens "Population density (pop/km$^2$)"

lab var renamu_pia "\textit{1. Municipal characteristics} \\ Municipal budget (2010 USD, million)"
lab var renamu_int "Internet connectivity"
lab var renamu_atform "Technical support"

global miss "pop_total_miss geo_pop_dens05_miss dis_piped_miss dis_hh_sewer_miss dis_hh_onsite_miss dis_hh_od_miss dis_educ_secp_miss dis_elecp_miss renamu_pia_miss renamu_int_miss renamu_atform_miss renamu_dhealth_miss"


*OLS: TREATMENT STATUS
*------------------------------------

reg in_sample  $muni $censo if year==2005, robust 
estimates store m1

reg vs_missno_max $muni $censo if year==2005, robust 
estimates store m2

esttab m1 m2 using "$tables/sample_OLS.tex", replace b(2) se(2) star f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses keep( $muni $censo) label collabels(none) 

est clear

**MR DATA AVAILABLE****

global treat "vs_missno"

rnd_check $muni $censo if year==2005

esttab using "$tables/sample_balance.tex", f replace nomtitles nonumb noeqlines nodepvars nogaps compress nolines noobs  mlabels(none) collabels(none) ///
cells("m_mu_1(fmt(2)) m_mu_2(fmt(2)) m_coef_0(fmt(2) star pvalue(m_d_p_0))" ///
"m_sd_1(fmt(2) par([ ])) m_sd_2(fmt(2) par([ ]))  m_d_se_0(fmt(2) par)") label  star(* 0.1 ** 0.05 *** 0.01)



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure B2: Distribution of mortality rates by cause of death
*Figure B3: Distribution of perinatal mortality rate (per 1,000 infants) by cause of death
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*SAMPLE: 
use "$data/analysis_district_clean_all", clear

*% AREA BAR GRAPH WITH DISTRIBUTION OF COD
*WATERBONE: INFECTIONS (A,B), PERINATAL (P), DIGESTIVE (K), ENDOCRINE, NUTTRITIONAL, METABOLIC (E)
**REFEREE 4: DIARROEA DISEASES (A,B) AND NUTRITIONAL DEFICIENCIES (E), NEONATAL DISORDERS (P)
egen mort_1y_tot=rowtotal(mort_1y_infec mort_1y_peri mort_1y_dige mort_1y_endoc mort_1y_resp mort_1y_accid mort_1y_other)
egen mort_u5y_tot=rowtotal(mort_u5y_infec mort_u5y_peri mort_u5y_dige mort_u5y_endoc mort_u5y_resp mort_u5y_accid mort_u5y_other)

global imr "mort_1y_infec mort_1y_peri mort_1y_dige mort_1y_endoc mort_1y_san mort_1y_resp mort_1y_accid mort_1y_other"
global u5mr "mort_u5y_infec mort_u5y_peri mort_u5y_dige mort_u5y_endoc mort_u5y_san mort_u5y_resp mort_u5y_accid mort_u5y_other"
global pop "pop_u5y"

collapse (sum) $imr $u5mr $pop mort_1y_tot mort_u5y_tot, by(year)

//share of perinatal from waterborne
cap drop mort_1y_tot2
gen mort_1y_tot2=mort_1y_peri/mort_1y_san
sum mort_1y_tot2

cap drop mort_u5y_tot2
gen mort_u5y_tot2=mort_u5y_peri/mort_u5y_san
sum mort_u5y_tot2
drop mort_1y_tot mort_u5y_tot

*ACCIDENTS: TRANSPORT ACCIDENTS (V), Other external causes of accidental injury (W), INTENTIONAL SELF-HARM (X), EVENT OF UNDETERMINEND INTENT, SEQUELS OF EXTERNAL CAUSES (Y)
//U5MR: Other external causes of accidental injury 15%, Accidental drowning and submersion 7%, Other accidental threats to breathing 27%,  Accidental exposure to other and unspecified factors (11%), 
//IMR: Other external causes of accidental injury 17%, Accidental drowning and submersion 7%, Other accidental threats to breathing 30%,  Accidental exposure to other and unspecified factors (12%), 

foreach var of varlist $imr {
    replace `var'=`var'/pop_u5y/5*$mr_per
}


foreach var of varlist $u5mr {
    replace `var'=`var'/pop_u5*$mr_per
}


*IMR COD GRAPH
*********************

lab var mort_1y_infec "Infectious and parasitic"
lab var mort_1y_peri "Perinatal period"
lab var mort_1y_dige "Digestive"
lab var mort_1y_endoc "Endocrine, nutrition and metabolic"
lab var mort_1y_accid "Accidents"
lab var mort_1y_resp "Respiratory"
lab var mort_1y_other "Other"

rename mort_1y_infec imr1
rename mort_1y_peri imr2
rename mort_1y_dige imr3
rename mort_1y_endoc imr4
rename mort_1y_accid imr5
rename mort_1y_resp imr6
rename mort_1y_other imr7

clonevar show1 = imr1 

forval j = 2/7 { 
    local k = `j' - 1 
    gen show`j' = show`k' + imr`j'
    _crcslbl show`j' imr`j'
}

local toshow 
forval j = 7(-1)1 { 
    local toshow `toshow' show`j'
}

twoway area `toshow'  year, xla(2005(2)2015) xtitle("") ////
legend(col(3) pos(6)) ytitle(IMR (per 100,000 infants)) 


graph bar imr*, over(year) stack percent ytitle(Percent) ylabel(0(10)100) ///
legend(order(1 "Infectious and parasitic" 2 "Perinatal period" 3 "Digestive" 4 "Endocrine, nutrition and metabolic" 5 "Accidents" 6 "Respiratory" 7 "Other") col(3) pos(6) size(small)) 

graph export "$graphs/perc_imr_cod.pdf", replace


*U5MR COD GRAPH
*********************

preserve

lab var mort_u5y_infec "Infectious and parasitic"
lab var mort_u5y_peri "Perinatal period"
lab var mort_u5y_dige "Digestive"
lab var mort_u5y_endoc "Endocrine, nutrition and metabolic  "
lab var mort_u5y_accid "Accidents"
lab var mort_u5y_resp "Respiratory"
lab var mort_u5y_other "Other"

rename mort_u5y_infec u5mr1
rename mort_u5y_peri u5mr2
rename mort_u5y_dige u5mr3
rename mort_u5y_endoc u5mr4
rename mort_u5y_accid u5mr5
rename mort_u5y_resp u5mr6
rename mort_u5y_other u5mr7

cap drop show*
clonevar show1 = u5mr1 

forval j = 2/7 { 
    local k = `j' - 1 
    gen show`j' = show`k' + imr`j'
    _crcslbl show`j' imr`j'
}

local toshow 
forval j = 7(-1)1 { 
    local toshow `toshow' show`j'
}

twoway area `toshow'  year, xla(2005(2)2015) xtitle("") ////
legend(col(3) pos(6)) ytitle(U5MR (per 100,000 children)) 

graph bar u5mr*, over(year) stack percent ytitle(Percent) ylabel(0(10)100) ///
legend(order(1 "Infectious and parasitic" 2 "Perinatal period" 3 "Digestive" 4 "Endocrine, nutrition and metabolic" 5 "Accidents" 6 "Respiratory" 7 "Other") col(3) pos(6))

graph export "$graphs/perc_u5mr_cod.pdf", replace

restore


*MR NEONATAL GRAPH
*********************
use "$data/analysis_district_clean", clear

collapse (sum) *peri* $pop, by(year)

cap drop peri_tot
gen peri_tot=mort_less1y_peri_P20_P29/mort_1y_peri
sum peri_tot


foreach var of varlist *peri* {
    replace `var'=`var'/pop_u5y*$mr_per
}

local mr "less1y"

foreach y of local mr {

lab var mort_`y'_peri_P00_P04 "Maternal and pregnancy"
lab var mort_`y'_peri_P05_P08 "Gestation and fetal"
lab var mort_`y'_peri_P10_P15 "Birth trauma"
lab var mort_`y'_peri_P20_P29 "Cardio, asphyxia, pneumonia"
lab var mort_`y'_peri_P35_P39 "Infections"
lab var mort_`y'_peri_P50_P61 "Hemorrage"
lab var mort_`y'_peri_P70_P74 "Endocrine and metabolic"
lab var mort_`y'_peri_P75_P78 "Digestive"
lab var mort_`y'_peri_P80_P83 "Temperature"
lab var mort_`y'_peri_P90_P96 "Other"

rename mort_`y'_peri_P00_P04 `y'1
rename mort_`y'_peri_P05_P08 `y'2
rename mort_`y'_peri_P10_P15 `y'3
rename mort_`y'_peri_P20_P29 `y'4 
rename mort_`y'_peri_P35_P39 `y'5
rename mort_`y'_peri_P50_P61 `y'6
rename mort_`y'_peri_P70_P74 `y'7 
rename mort_`y'_peri_P75_P78 `y'8
rename mort_`y'_peri_P80_P83 `y'9
rename mort_`y'_peri_P90_P96 `y'10

cap drop show*
clonevar show1 = `y'1 

forval j = 2/7 { 
    local k = `j' - 1 
    gen show`j' = show`k' + `y'`j'
    _crcslbl show`j' `y'`j'
}

local toshow 
forval j = 7(-1)1 { 
    local toshow `toshow' show`j'
}

twoway area `toshow'  year, xla(2005(2)2015) xtitle("") ////
legend(col(3) pos(6)) ytitle(Neo-natal mortality (per 100,000 children)) 


graph bar `y'1-`y'10, over(year) stack percent ytitle(Percent) ylabel(0(10)100) ///
legend(order(1 "Maternal and pregnancy" 2 "Gestation and fetal" 3 "Birth trauma" 4  "Cardio, asphyxia, pneumonia" 5 "Infections" 6 "Hemorrage" 7 "Endocrine and metabolic" 8 "Digestive" 9 "Temperature" 10 "Other") col(3) pos(6))

graph export "$graphs/perc_neo_`y'.pdf", replace

}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table B5: Data sources and average values across districts in the analysis
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

//Globals	
global mr "vs_imr1y vs_u5mr"
global censo "inei_pop0 geo_pop_dens dis_piped0 dis_hh_sewer0 dis_hh_onsite0 dis_hh_od0 dis_educ_secp0 dis_elecp0"
global muni "renamu_pia0 renamu_int0 renamu_atform0 renamu_dhealth0" 

//Initial (2005)
lab var vs_imr1y "\textit{1. Outcomes} \\ IMR (per 1,000 infants)"			
lab var vs_u5mr "U5MR (per 1,000 children)"		

lab var inei_pop0 "\textit{2. District characteristics} \\ Population"
lab var dis_piped0 "Share HH piped water"
lab var geo_pop_dens "Population density ($\frac{pop}{km^2}$)"
lab var dis_educ_secp0 "Share HH head secondary"
lab var dis_elecp0 "Share HH electricity"

lab var renamu_pia0 "\textit{3. Municipal characteristics} \\ Municipal budget (2010 USD, million)"
lab var renamu_int0 "Internet connectivity"
lab var renamu_atform0 "Technical support"

lab var geo_slope2_perc "\textit{1. Geographical features} \\ Share district gradient (0.8, 4.19]\%"
lab var geo_slope3_perc "Share district gradient (4.19, 13]\%"
lab var geo_slope4_perc "Share district gradient above 13\%"

lab var geo_elev_251500perc "Share district elevation (250, 500] mamsl"
lab var geo_elev_5011000perc "Share district elevation (500, 1000] mamsl"
lab var geo_elev_1001perc "Share district elevation above 1000 mamsl"

lab var geo_area "District area (km$^2$)"
lab var river_leng "River density ($\frac{km}{km^2}$)"


eststo sum2005: estpost summarize  $mr $censo $muni  if year==2005 
eststo sum2015: estpost summarize   $mr $censo $muni if year==2015

esttab  sum2005 sum2015 using "$tables/descriptab.tex", f replace ///
	nomtitles nonumb noeqlines nodepvars nogaps compress nolines noobs mlabels(none) collabels(none) ///
		cells("mean(pattern(1 1) fmt(2)) sd(pattern(1 1) fmt(2))") ///
		label star(* 0.1 ** 0.05 *** 0.01) nonotes
		eststo clear
	
eststo clear


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% C Additional material for Section III, Empirical strategy
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C1: Distribution of start and completion year across districts
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear

//Within districts that implemented sewerage works
preserve
collapse (min) s0_year s2_year, by(ubigeo_id)

eststo m1:  estpost tab s0_year
eststo m2:  estpost tab s2_year if s0_year!=.

esttab m1 m2 using "$tables/tab_s0year.tex", replace ///
cell("b pct(fmt(2))")  collab(none) noobs nonumb nomtitle ///
f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses 

restore


//Across all districts
gen treat_never=s0_year==.
gen complete_never=s2_year==.

preserve
collapse (min) treat_never complete_never, by(ubigeo_id)

lab def never 0 "Ever" 1 "Never"
lab values treat_never never
lab values complete_never never

eststo m1:  estpost tab treat_never
eststo m2:  estpost tab complete_never if treat_never==0

esttab m1 m2 using "$tables/tab_s0year_never.tex", replace ///
cell("b pct(fmt(2))")  collab(none) noobs nonumb nomtitle ///
f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses 

restore



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C2: Distribution of start and completion year across projects
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

loaddata_proj

*Years projects started and completed
preserve
collapse (min) p_sewerage_y0y p_sewerage_y2y, by(ubigeo snip)

eststo m1:  estpost tab p_sewerage_y0y
eststo m2:  estpost tab p_sewerage_y2y if p_sewerage_y0y!=.

esttab m1 m2 using "$tables/tab_p_s0year.tex", replace ///
cell("b pct(fmt(2))")  collab(none) noobs nonumb nomtitle ///
f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses 

restore


*Ever completed across all projects
gen complete_never=p_sewerage_y2y==.

preserve
collapse (min) complete_never, by(ubigeo snip)

lab def never 0 "Ever" 1 "Never"
lab values complete_never never

eststo m3:  estpost tab complete_never 
eststo m4:  estpost tab complete_never 

esttab m3 m4 using "$tables/tab_p_s0year_never.tex", replace ///
cell("b pct(fmt(2))")  collab(none) noobs nonumb nomtitle ///
f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses 

restore



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C3: Distribution of start and completion year across projects, by type of project
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

preserve

collapse (min) p_sewerage_y0y p_sewerage_y2y (max) p_type_cat, by(ubigeo snip)

eststo m1:  estpost tab p_sewerage_y0y if p_type_cat==1
eststo m2:  estpost tab p_sewerage_y2y if p_sewerage_y0y!=. & p_type_cat==1

eststo m3:  estpost tab p_sewerage_y0y if p_type_cat>1 & p_type_cat!=.
eststo m4:  estpost tab p_sewerage_y2y if p_sewerage_y0y!=. & p_type_cat>1 & p_type_cat!=.


esttab m1 m2 m3 m4 using "$tables/tab_ptype_s0year.tex", replace ///
cell("b pct(fmt(2))")  collab(none) noobs nonumb nomtitle ///
f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses 

restore




*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure C1: Spatial distribution of start year
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", replace

replace s0_year=0 if s0_year==.

collapse (max) s0_year pscore, by(ubigeo)

tempfile min_first_year
save `min_first_year'

use "$data/GIS/data_polygons.dta",clear
drop UBIGEO
rename IDDIST ubigeo
merge 1:1 ubigeo using `min_first_year', keep(3)
tempfile data_polygons_min
save `data_polygons_min'

preserve
spmap s0_year using "$data/GIS/coor_polygons.dta", id(_ID) clmethod(unique)  ocolor(gs10 ..) osize(0.03 ..) ndocolor(gs10 ..) fcolor(Blues2) //Greens2
graph export "$graphs/event1_map.pdf", replace
restore

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure F4: Spatial distribution of geographic suitability
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


	preserve
	spmap pscore using "$data/GIS/coor_polygons.dta", id(_ID) ocolor(gs10 ..) osize(0.03 ..) ndocolor(gs10 ..) 	 fcolor(Blues2) //Greens2
	graph export "$graphs/suitable_map.pdf", replace
	restore		


	
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C4: Treatment status and timing of start
*Table C8: Cox hazard model: predicting timing of start with pre-start changes
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


//Globals				
global mr "vs_imr1y vs_u5mr vs_imr1y_chg vs_u5mr_chg" 
global mr_prechgm "vs_imr1y_prechgm vs_u5mr_prechgm" 
global censo "inei_pop geo_pop_dens05 dis_piped dis_hh_sewer dis_hh_onsite dis_hh_od dis_educ_secp dis_elecp"
global muni "renamu_pia renamu_int renamu_atform renamu_dhealth" 
global muni_prechgm "inei_pop_prechgm geo_pop_dens_prechgm renamu_pia_prechgm renamu_int_prechgm renamu_atform_prechgm renamu_dhealth_prechgm"

global pol "pct_reelection_1993"
global pol_prechgm "turnover_prechgm"

global mr "vs_imr1y vs_u5mr"


//Labels
lab var vs_imr1y_chg "$\triangle$ IMR (2005-2015)"
lab var vs_u5mr_chg "$\triangle$ U5MR (2005-2015)"

lab var vs_imr1y "\textit{1. Outcomes} \\ IMR (per 1,000 infants)"			
lab var pop_u5y_1y "Infants"
lab var vs_u5mr "U5MR (per 1,000 children)"	
lab var pop_u5y	"Under-5 population"	

lab var inei_pop "\textit{2. District characteristics} \\ Population"
lab var dis_piped "Share HH piped water"
lab var geo_pop_dens "Population density (pop/km$^2$)"

lab var renamu_pia "\textit{3. Municipal characteristics} \\ Municipal budget (2010 USD, million)"
lab var renamu_int "Internet connectivity"
lab var renamu_atform "Technical support"

lab var geo_slope2_perc "\textit{1. Geographical features} \\ Share district gradient (0.8, 4.19]\%"
lab var geo_slope3_perc "Share district gradient (4.19, 13]\%"
lab var geo_slope4_perc "Share district gradient above 13\%"

lab var geo_elev_251500perc "Share district elevation (250, 500] mamsl"
lab var geo_elev_5011000perc "Share district elevation (500, 1000] mamsl"
lab var geo_elev_1001perc "Share district elevation above 1000 mamsl"

lab var geo_area "District area (km$^2$)"
lab var river_leng "River density ($\frac{km}{km^2}$)"

//Pre-start change
lab var vs_imr1y_prechgm "\textit{1. Outcomes} \\ $\triangle$ IMR"
lab var vs_u5mr_prechgm "$\triangle$ U5MR"

lab var inei_pop_prechgm "\textit{2. District characteristics} \\ $\triangle$ Population"
lab var geo_pop_dens_prechgm " $\triangle$ Population density"

lab var renamu_pia_prechgm "\textit{3. Municipal characteristics} \\ $\triangle$ Municipal budget"
lab var renamu_int_prechgm "$\triangle$ Internet connectivity"
lab var renamu_atform_prechgm "$\triangle$ Technical support"
lab var renamu_dhealth_prechgm "$\triangle$ Manages health centers"
lab var turnover_prechgm "$\triangle$ Political turnover"


*T STATUS AND COX HAZARD MODEL
*----------------------------------------
global miss "pop_total_miss geo_pop_dens05_miss dis_piped_miss dis_hh_sewer_miss dis_hh_onsite_miss dis_hh_od_miss dis_educ_secp_miss dis_elecp_miss renamu_pia_miss renamu_int_miss renamu_atform_miss renamu_dhealth_miss"

global xlist_geo "geo_slope2_perc geo_slope3_perc geo_slope4_perc geo_elev_251500perc geo_elev_5011000perc geo_elev_1001perc geo_area river_leng geo_region1 geo_region2"



*OLS: TREATMENT STATUS
*------------------------------------

reg treat_y0 $xlist_geo $censo $muni if year==2005, robust 
estimates store m1

reg treat_y0 $xlist_geo $censo $muni $pol if year==2005, robust 
estimates store m2


*COX-HAZARD MODEL: SETTING UP DATA
*------------------------------------

global time spell
global event event

preserve

//For 2005: 
foreach var of varlist $censo $muni $pol {
	replace `var'=. if year!=2005
}


//Time
gen spell=1 if year<=s0_year

//Event 
gen event=s0_year==year 

 foreach v of var * {
 	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
  	}
  }

collapse (sum) spell (max) event (mean) $xlist_geo $censo $muni $pol  $mr_prechgm $muni_prechgm $pol_prechgm (max) treat_y0, by(ubigeo_id)


foreach v of var * {
 	label var `v' `"`l`v''"'
  }


//Shape data as survival time
stset $time, failure($event)
stdescribe
stsum

*Non-parametric estimation
**Graph of hazard ration
sts graph, title("") graphregion(color(white)) xtitle("Years until start of implementation") xlabel(0(2)10) title("Hazard: start of implementation phase")
graph export "$graphs/hazard_start.pdf", replace

*Parametric model (Cox) - cross-sectional
stcox $xlist_geo $censo $muni if treat_y0==1, nohr
estimates store m3

stcox $xlist_geo $censo $muni $pol if treat_y0==1, nohr
estimates store m4

esttab m1 m2 m3 m4 using "$tables/hazard_model_start.tex", replace b(2) se(2) star f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses keep($xlist_geo $censo $muni $pol) label collabels(none) 

est clear

*Parametric model (Cox) - pre-start changes

stcox $mr_prechgm if treat_y0==1, nohr
estimates store m1pre

stcox $mr_prechgm $muni_prechgm if treat_y0==1, nohr
estimates store m2pre

stcox $mr_prechgm $muni_prechgm $pol_prechgm if treat_y0==1, nohr
estimates store m3pre

esttab m1pre m2pre m3pre using "$tables/hazard_model_start_prechgm.tex", replace b(2) se(2) star f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses keep($mr_prechgm $muni_prechgm $pol_prechgm) label collabels(none) 

restore

est clear


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C5: Balance in initial characteristics (2005), by ‘pure’ control and treatment groups
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global treat "treat_y0"

lab var vs_imr1y "C vs. T (by `y')"

lab var inei_pop "\textit{1. District characteristics} \\ Population"
lab var renamu_pia "\textit{2. Municipal characteristics} \\ Municipal budget (2010 USD, million)"

rnd_check $censo $muni if year==2005

esttab using "$tables/balance.tex", f replace nomtitles nonumb noeqlines nodepvars nogaps compress nolines noobs  mlabels(none) collabels(none) ///
cells("m_mu_1(fmt(2)) m_mu_2(fmt(2)) m_coef_0(fmt(2) star pvalue(m_d_p_0))" ///
"m_sd_1(fmt(2) par([ ])) m_sd_2(fmt(2) par([ ]))  m_d_se_0(fmt(2) par)") label  star(* 0.1 ** 0.05 *** 0.01)


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table C6: Initial IMR levels, by treatment and control groups (dynamic)
*Table C7: Initial U5MR levels, by treatment and control groups (dynamic)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all.dta", clear

cap drop treatment*

gen treatment2005=s0_year==2005

forvalues y=2006/2015{
gen treatment`y'=s0_year<=`y'

bysort ubigeo_id: egen treatment`y'max=max(treatment`y')
drop treatment`y'
rename treatment`y'max treatment`y'
}

keep if year==2005


erase "$tables/balance_TC_imr.tex"
erase "$tables/balance_TC_u5mr.tex"

*IMR in levels
*----------------
forvalues y=2005/2015 {
	
global treat "treatment`y'"

lab var vs_imr1y "C vs. T (by `y')"

rnd_check vs_imr1y


esttab using "$tables/balance_TC_imr.tex", f append nomtitles nonumb noeqlines nodepvars nogaps compress nolines noobs  mlabels(none) collabels(none) ///
cells("m_mu_1(fmt(2)) m_mu_2(fmt(2)) m_coef_0(fmt(2) star pvalue(m_d_p_0))" ///
"m_sd_1(fmt(2) par([ ])) m_sd_2(fmt(2) par([ ]))  m_d_se_0(fmt(2) par)") label  star(* 0.1 ** 0.05 *** 0.01)

}


*U5MR in levels
*----------------
forvalues y=2005/2015 {

global treat "treatment`y'"

lab var vs_u5mr "C vs. T (by `y')"

rnd_check vs_u5mr

esttab using "$tables/balance_TC_u5mr.tex", f append nomtitles nonumb noeqlines nodepvars nogaps compress nolines noobs  mlabels(none) collabels(none) ///
cells("m_mu_1(fmt(2)) m_mu_2(fmt(2)) m_coef_0(fmt(2) star pvalue(m_d_p_0))" ///
"m_sd_1(fmt(2) par([ ])) m_sd_2(fmt(2) par([ ]))  m_d_se_0(fmt(2) par)") label  star(* 0.1 ** 0.05 *** 0.01)

}




*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% D Additional material for Section IV.2, Robustness checks
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure D1: Effect on infant mortality rate (alternative denominator)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

qui {

foreach y of varlist vs_imr1yA  { 
	
// OLS
reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event
estimates store ols

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

event_plot ols cs, stub_lag(L#event Tp#) stub_lead(F#event Tm#) plottype(scatter) ciplottype(rcap)  ///
together perturb(0(0.2)0.4) trimlead(6) trimlag(7) noautolegend ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr /// 
legend(order(1 "OLS" 3 "Callaway-Sant'Anna (2021)") rows(1) pos(12)) name(gimr1, replace)) ///
		lag_opt1(color(navy) msymbol(S)) lag_ci_opt1(color(navy%60 navy%60) lwidth(thick)) ///
		lag_opt2(color(navy) msymbol(Th)) lag_ci_opt2(color(navy%60 navy%60) lwidth(medthick) lp(dash)) ///
		alpha(0.1)
graph export "$graphs/event1_`y'.pdf", replace

est clear

}
}

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table D1: Dynamic effect on infant mortality rate (alternative denominator), by cause of death
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear


drop *event*

//Get leads and lags
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event
gen F1event=0

lab var L1event "0 to 3"
lab var L2event "4 to 7"
lab var L3event "8 to 10"
lab var F2event "--10 to --2"

lab var F2event "Pre (--10 to --2)"
lab var L1event "Post 1 (0 to 3)"
lab var L2event "Post 2 (4 to 7)"
lab var L3event "Post 3 (8 to 10)"	
		
//MR and MR by COD		
		
qui{
	
local count = 1
foreach y of varlist vs_imr1yA vs_imr1yA_san vs_imr1yA_accid vs_imr1yA_resp vs_imr1yA_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 
stats_table_a `y'
stats_table_pvalue `y'

}

		local count = `count' + 1
		
}


}


esttab ols1 ols2 ols3 ols4 ols5 using "$tables/event1_agg_MRCODA.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

eststo clear

				
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure D2: Effect on infant mortality rate (alternative denominator), by cause of death
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		
		
use "$data/analysis_district_clean_all", clear

qui{
	
local count = 1
foreach y of varlist vs_imr1yA_san vs_imr1yA_accid vs_imr1yA_resp vs_imr1yA_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 

}

		local count = `count' + 1
		
}

event_plot ols1 ols2 ols3 ols4, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr2 legend(order(1 "Waterborne" 3 "Accidents" 5 "Respiratory" 7 "Other") rows(1))) ///
together perturb(-0.6(0.2)0.6)  noautolegend ///
 trimlead(6) trimlag(7) ///
	lag_opt1(msymbol(O) color(navy)) lag_ci_opt1(color(navy) lwidth(medium))  ///
	lag_opt2(msymbol(T) color(cranberry)) lag_ci_opt2(color(cranberry) lwidth(medium))  ///
	lag_opt3(msymbol(Oh) color(forest_green)) lag_ci_opt3(color(forest_green) lwidth(medium))   ///
	lag_opt4(msymbol(X) color(gs8)) lag_ci_opt4(color(gs8) lwidth(medium)) alpha(0.1)
	
graph export "$graphs/event1_IMR_codA.pdf", replace

est drop ols1 ols2 ols3 ols4


}
		
						
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure D3: Decomposition of static TWFE estimator
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		

use "$data/analysis_district_clean_all", clear

label var D "Implementation"

xtset  i t

foreach y of varlist vs_imr1y vs_u5mr {
	
xtreg `y' D year_1-year_10, fe cl(i)

bacondecomp `y' D year_1-year_10, stub(`y') robust

graph save "$graphs/bacondecomp_`y'.gph", replace

}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table D2: Dynamic effect by Callaway and Sant’Anna (2021)
*Table D3: Other aggregate estimands by Callaway and Sant’Anna (2021)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		
use "$data/analysis_district_clean_all", clear


*EVENT STUDY
*---------------------------------

local count = 1
foreach y of varlist vs_imr1y vs_u5mr {
	
if `count' == 1 | `count' == 2  {

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2 
estat event, estore(cs`count') // this produces and stores the estimates at the same time


}

		local count = `count' + 1
		
}


esttab cs1 cs2 using "$tables/event1_CS_MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) noobs
							
est clear



*SIMPLE
*---------------------------------

local count = 1
foreach y of varlist vs_imr1y vs_u5mr {
	
if `count' == 1 | `count' == 2  {

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(simple) 
estat simple, estore(cs`count') // this produces and stores the estimates at the same time


}

		local count = `count' + 1
		
}


esttab cs1 cs2 using "$tables/simple_CS_MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) noobs
							
est clear


*CALENDAR: CUMMULATIVE
*---------------------------------

local count = 1
foreach y of varlist vs_imr1y vs_u5mr {
	
if `count' == 1 | `count' == 2  {

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(calendar) 
estat calendar, estore(cs`count') // this produces and stores the estimates at the same time


}

		local count = `count' + 1
		
}


esttab cs1 cs2 using "$tables/calendar_CS_MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) noobs
							
est clear



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure D4: Effect of infrastructure development on infant mortality rate, by cause of death
*Figure D5: Effect of infrastructure development on under-5 mortality rate, by cause of death
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

*ALL PERIODS
*---------------------------------

qui {

foreach y of varlist vs_imr1y_san vs_imr1y_accid   { 
	
// OLS
reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event
estimates store ols

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

event_plot ols cs, stub_lag(L#event Tp#) stub_lead(F#event Tm#) plottype(scatter) ciplottype(rcap)  ///
together perturb(0(0.2)0.4) trimlead(6) trimlag(7) noautolegend ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr /// 
legend(order(1 "OLS" 3 "Callaway-Sant'Anna (2021)") rows(1) pos(12))) ///
		lag_opt1(color(navy) msymbol(S)) lag_ci_opt1(color(navy%60 navy%60) lwidth(thick)) ///
		lag_opt2(color(navy) msymbol(Th)) lag_ci_opt2(color(navy%60 navy%60) lwidth(medthick) lp(dash)) ///
		alpha(0.1)
graph export "$graphs/event1_`y'.pdf", replace

est clear

}


foreach y of varlist vs_u5mr_san vs_u5mr_accid  {

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

estimates store ols

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

event_plot ols cs, stub_lag(L#event Tp#) stub_lead(F#event Tm#) plottype(scatter) ciplottype(rcap)  ///
together perturb(0(0.2)0.4) trimlead(6) trimlag(7) noautolegend ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr /// 
legend(order(1 "OLS" 3 "Callaway-Sant'Anna (2021)") rows(1) pos(12))) ///
		lag_opt1(color(navy) msymbol(S)) lag_ci_opt1(color(navy%60 navy%60) lwidth(thick)) ///
		lag_opt2(color(navy) msymbol(Th)) lag_ci_opt2(color(navy%60 navy%60) lwidth(medthick) lp(dash)) ///
		alpha(0.1)
graph export "$graphs/event1_`y'.pdf", replace

est clear

}
}


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure D6: Robustness check: alternative TWFE estimators
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

qui{
	
foreach y of varlist vs_imr1y vs_u5mr { //

// Estimation with did_multiplegt of de Chaisemartin and D'Haultfoeuille (2020)
did_multiplegt `y' i t D, robust_dynamic breps(100) cluster(i) dynamic(7) placebo(5)
	
matrix dcdh_b = e(estimates) // storing the estimates for later
mat li dcdh_b
matrix rownames dcdh_b = "Effect_0" "Effect_1" "Effect_2" "Effect_3" "Effect_4" "Effect_5" "Effect_6" "Effect_7" "Average" "Placebo_2" "Placebo_3" " Placebo_4" " Placebo_5" " Placebo_6"

matrix dcdh_v = e(variances)
mat li dcdh_v
matrix rownames dcdh_v = "Effect_0" "Effect_1" "Effect_2" "Effect_3" "Effect_4" "Effect_5" "Effect_6" "Effect_7" "Average" "Placebo_2" "Placebo_3" " Placebo_4" " Placebo_5" " Placebo_6"	


// Estimation with cldid of Callaway and Sant'Anna (2020)
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

// Estimation with eventstudyinteract of Sun and Abraham (2020)
eventstudyinteract `y' L*event F*event, vce(cluster i) absorb(i t) cohort(Ei) control_cohort(lastcohort)
estimates store sa

matrix sa_b = e(b_iw) // storing the estimates for later
matrix sa_v = e(V_iw)

// TWFE OLS estimation (which is correct here because of treatment effect homogeneity). Some groups could be binned.
reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols // saving the estimates for later

// Combine all plots using the stored estimates
event_plot  dcdh_b#dcdh_v cs sa_b#sa_v ols, ///ols
	stub_lag(Effect_# Tp# L#event L#event) stub_lead(Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.6(0.2)0.6) trimlead(6) trimlag(7) noautolegend ///
	graph_opt(title("") ///
		xtitle("Years since start of implementation") ytitle("Average causal effect") xlabel(-6(1)7)  ///
		legend(order(1 "de Chaisemartin-D'Haultfoeuille" ///
				3 "Callaway-Sant'Anna" 5 "Sun-Abraham" 7 "OLS") rows(2) region(style(none))) ///
	/// the following lines replace default_look with something more elaborate
		xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(Oh) color(dknavy)) lag_ci_opt1(color(dknavy)) ///
	lag_opt2(msymbol(Th) color(midblue)) lag_ci_opt2(color(midblue)) ///
	lag_opt3(msymbol(Sh) color(ebblue)) lag_ci_opt3(color(ebblue)) ///
	lag_opt4(msymbol(O) color(navy)) lag_ci_opt4(color(navy)) alpha(0.1)
graph export "$graphs/event_5est_`y'.pdf", replace

}

est clear

}


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table D4: Dynamic effect on infant mortality, additional controls
*Table D5: Dynamic effect on under-5 mortality, additional controls
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


drop *event*

//Get leads and lags
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event

lab var F2event "Pre (--10 to --2)"
lab var L1event "Post 1 (0 to 3)"
lab var L2event "Post 2 (4 to 7)"
lab var L3event "Post 3 (8 to 10)"

//Controls
global control2 "pscore2y" 
global control3 "geo_region1y geo_region2y geo_region3y"
global control4  "inei_pop geo_pop_dens"
global control5 "$muni"
global control6 "siaf_ejc_energ_ln siaf_ejc_salud_ln siaf_ejc_trans_ln"		
global control7 "turnover"

	
qui {
foreach y of varlist vs_imr1y vs_u5mr { 
	
reghdfe `y' F*event L*event, a(i t) cluster(i) 

preserve

keep if e(sample)==1

reghdfe `y' F*event L*event $control2, a(i t) cluster(i) 
estimates store `y'2
stats_table_a `y'
stats_table_pvalue `y'

reghdfe `y' F*event L*event $control3, a(i t) cluster(i) 
estimates store `y'3
stats_table_a `y'
stats_table_pvalue `y'

reghdfe `y' F*event L*event $control4, a(i t) cluster(i) 
estimates store `y'4
stats_table_a `y'
stats_table_pvalue `y'

reghdfe `y' F*event L*event $control5, a(i t) cluster(i) 
estimates store `y'5
stats_table_a `y'
stats_table_pvalue `y'

reghdfe `y' F*event L*event $control6, a(i t) cluster(i) 
estimates store `y'6
stats_table_a `y'
stats_table_pvalue `y'

reghdfe `y' F*event L*event $control7, a(i t) cluster(i) 
estimates store `y'7
stats_table_a `y'
stats_table_pvalue `y'

restore

}
}

esttab vs_imr1y* using "$tables/event1_rob_agg_imr.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	
		
esttab vs_u5mr* using "$tables/event1_rob_agg_u5mr.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	
		
eststo clear


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure E1: Effect on under-5 population
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


*DYNAMIC EVENT WITH POPULATION FORECASTS
*----------------------------------------------	

*ALL PERIODS
*--------------------

qui{
foreach y of varlist pop_u5y_1y pop_u5y { 

reghdfe `y' F*event L*event, a(i t) cluster(i) 

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("") ytitle("Average causal effect") title("") xlabel(-6(1)7) legend(order(1 "Pre-trend" 3 "Implementation")) name(`y', replace)) ///
 trimlead(6) trimlag(7) together ///
		lag_opt(color(navy) msymbol(S)) ///
		lag_ci_opt(color(navy%60 navy%60) lwidth(thick)) ///
		
	graph export "$graphs/event1_agg`y'.pdf", replace

}
}

*AGGREGATE
*--------------------	
	
drop *event*

//Get leads and lags
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event
gen F1event=0

lab var L1event "0 to 3"
lab var L2event "4 to 7"
lab var L3event "8 to 10"
lab var F2event "-2 to -6"


lab var pop_u5y_1y "A. Infants"
lab var pop_u5y "B. Under-five population"
lab var inei_pop "C. Population"
lab var geo_pop_dens "D. Population density (sq.km)"


qui{
foreach y of varlist pop_u5y_1y pop_u5y { 
	
	local lab: variable label `y'
	
reghdfe `y' F*event L*event, a(i t) cluster(i) 
estimates store m2
stats_table_a `y'
stats_table_pvalue `y'
	

reghdfe `y' F*event L*event, a(i t) cluster(i) 
estimates store m1 
stats_table_a `y'
stats_table_pvalue `y'


event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
title("") xtitle("") ytitle("")  xscale(range(-3 4))  xlabel(-2 "-10to-2" -1 " " 1 "0to3" 2 "4to7" 3 "8to10" ) legend(order(1 "Pre-trend" 3 "Implementation")) fxsize(80) name(`y'2, replace)) together ///
		lag_opt(color(maroon) msymbol(O)) ///
		lag_ci_opt(color(maroon%60 maroon%60) lwidth(thick)) alpha(0.1)

}


		graph combine pop_u5y_1y pop_u5y_1y2, cols(2) graphregion(color(white) margin(zero)) b1("Years since start of implementation") 
		graph display, xsize(2) ysize(1)
		graph export "$graphs/event1_agg_pop1y.pdf", replace
		
		graph combine pop_u5y pop_u5y2, cols(2) graphregion(color(white) margin(zero)) b1("Years since start of implementation") 
		graph display, xsize(2) ysize(1)
		graph export "$graphs/event1_agg_pop5y.pdf", replace

}

est clear



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table E1: Effect of infrastructure development on demographics and behaviour
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear



label var D "Implementation"

qui{
	
local count = 1
foreach y of varlist pop_total dis_educ_secp0 dis_piped0 dis_hh_sewer0  { 
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 | `count' == 6 | `count' == 7  {
		
reghdfe `y' D, a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD_behav.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))		

est clear

}


*Placebo test for pre-trends
*-----------------------------

cap drop treat 
cap drop treat_never
gen treat_never=s0_year==.
gen treat=s0_year!=.

drop if s0_year<2007 //drop those treated before 2007

gen year05=year==2005
gen year07=year==2007
gen year15=year==2015

gen D2005=treat*year05
gen D2007=treat*year07
gen D2015=treat*year15

lab var D2005 "Pre (2005)"
lab var D2015 "Post (2017)"

qui{
	
local count = 1
foreach y of varlist pop_total dis_educ_secp0 dis_piped0 dis_hh_sewer0  { 
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 | `count' == 6 | `count' == 7  {
		
reghdfe `y' D2005 D2015, a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD_behav2.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D20*) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))		

est clear

}




*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% F Additional material for Section IV.4.i, Heterogeneous effects by intensity
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table F1: Geographic characteristics affecting sewerage implementation
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear

lab var geo_pop_dens05 "Population density (per sq km)"
global cost1 "geo_slope2_perc geo_slope3_perc geo_slope4_perc geo_elev_251500perc geo_elev_5011000perc geo_elev_1001perc river_leng geo_area"

lab var geo_slope2_perc "Share district gradient \textbraceleft{0.8, 4.19]}\%"
lab var geo_slope3_perc "Share district gradient \textbraceleft{4.19, 13]}\%"
lab var geo_slope4_perc "Share district gradient above 13\%"

lab var geo_elev_251500perc "Share district elevation \textbraceleft{250, 500]} mamsl"
lab var geo_elev_5011000perc "Share district elevation \textbraceleft{500, 1000]} mamsl"
lab var geo_elev_1001perc "Share district elevation above 1000 mamsl"
lab var geo_area "District area (km$^2$)"
lab var river_leng "River density ($\frac{km}{km^2}$)"

		cap drop pscore
		eststo model1: reg s0 $cost1 if year==2015, robust beta //fixed geographic characteristics
		estout  model1 using ///
		"$tables/lcmodel.tex", replace ///
		cells(b(fmt(%9.2f)) se(par fmt(%9.2f)) p(par([ ]) fmt(%9.2f)))  ///
		drop(_cons) label style(tex) mlabels(none) collabels(none) 
		eststo clear


		
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure F1: Event study of the number of open projects
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


qui {
foreach y of varlist s1 {

reg `y' F*event L*event, cluster(i) 

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Open projects") title("") $xaxis_start  ylabel(0(2)10) legend(order(1 "Pre-start" 3 "Implementation"))) ///		
 trimlead(6) trimlag(7) together  ///
		lag_opt(color(navy) msymbol(S)) ///
		lead_opt(color(navy)) ///
		lag_ci_opt(color(navy%45 navy%45)) ///
		lead_ci_opt(color(navy%45 navy%45))

graph export "$graphs/event1_`y'.pdf", replace

}
}

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure F2: Geographic suitability and projects, district level
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cap drop s1miss
gen s1miss=s1
replace s1miss=. if s1==0

cap drop duration
bysort ubigeo: egen duration=count(s1miss)

		egen byte d = cut(pscore), group(30)
		set type double
		
		collapse (max) pscore = pscore s0=s0 d duration s2 (mean) s1=s1 p_aband = p_aband , by(ubigeo_id)		
		
		gen s2_s0=s2/s0
		
		collapse (mean) pscore = pscore duration = duration p_aband = p_aband s0=s0 s1=s1 s2 s2_s0, by(d)
		
		*Duration
		twoway scatter s0 pscore, mcolor(navy)  || lfit s0 pscore, lcolor(navy) || scatter s1 pscore, mcolor(green)  || lfit s1 pscore, lcolor(green) lp(dash) ///
		, graphregion(color(white)) xtitle("Geographic suitability for sewerage") legend(order(2 "Started projects" 4 "Open projects") col(3))
		
		corr s0 pscore 
		
		graph export "$graphs/dis_suitable.pdf", replace
		

	
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure F3: Geographic suitability and projects, project level
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

loaddata_proj

egen byte d = cut(pscore), group(30)
set type double
		
collapse (max) d p_sewerage_ts0 p_sewerage_y2 pscore, by(ubigeo_id snip)	


collapse (mean) p_sewerage_ts0 p_sewerage_y2 pscore, by(d)	

twoway scatter p_sewerage_ts0 pscore, mcolor(navy)  || lfit p_sewerage_ts0 pscore, lcolor(navy) || scatter p_sewerage_y2 pscore,  yaxis(2) mcolor(cranberry)  || lfit p_sewerage_y2 pscore,  yaxis(2) lcolor(cranberry) lp(dash)   ///
, graphregion(color(white)) xtitle("Geographic suitability for sewerage") legend(order(2 "Years until start" 4 "Ever completed" ) col(3))
		
		graph export "$graphs/proj_suitable.pdf", replace

	
	
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table F2: Dynamic effect of infrastructure development on early-life mortality, high intensity
*Table F3: Dynamic effect of infrastructure development on early-life mortality, low intensity
*Table F4:‘Horse-race’-like heterogeneous effects
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		
use "$data/analysis_district_clean_all", clear

foreach het of varlist pscore pop05_total hh05_piped hh05_sewer geo_pop_dens05 dis_educ_secomp05 dis_elecp05 renamu_pia05 pct_reelection_199305 { 
	cap drop `het'm 
	cap drop *event_`het'
	
	sum `het' if year ==2015, det
	gen `het'm=`het'>=`r(p50)' if `het'!=.
	gen D_`het'=D*`het'm


lab var D "Implementation"
}


foreach het2 of varlist renamu_int05 renamu_atform05 renamu_dhealth05 {
	gen D_`het2'=D*`het2'
}


lab var D_pscore "Implementation  $\times$ High intensity"
lab var D_pop05_total "Implementation  $\times$ Population"
lab var D_hh05_piped "Implementation  $\times$ Piped water"
lab var D_hh05_sewer "Implementation  $\times$ Sewerage"
lab var D_geo_pop_dens05  "Implementation  $\times$ Pop density"
lab var D_dis_educ_secomp05 "Implementation  $\times$ Head secondary"
lab var D_dis_elecp05 "Implementation  $\times$ Electricity"
lab var D_renamu_pia05 "Implementation  $\times$ Municipal budget"
lab var D_renamu_int05 "Implementation  $\times$ Municipal connectivity"
lab var D_renamu_atform05 "Implementation  $\times$ Municipal technical support"
lab var D_renamu_dhealth05 "Implementation  $\times$ Municipality manages health"
lab var D_pct_reelection_199305 "Implementation  $\times$ Stronghold"


drop *event*

//Get leads and lags
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event

lab var L1event "0 to 3"
lab var L2event "4 to 7"
lab var L3event "8 to 10"
lab var F2event "--10 to --2"

lab var F2event "Pre (--10 to --2)"
lab var L1event "Post 1 (0 to 3)"
lab var L2event "Post 2 (4 to 7)"
lab var L3event "Post 3 (8 to 10)"

		

forvalues n=0/1 {
	
preserve 

keep if pscorem==`n'
		
qui{
	
local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4  | `count' == 5 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 
stats_table_a `y'
stats_table_pvalue `y'

}

		local count = `count' + 1
		
}


	
local count = 6
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other {
	
	if  `count' == 6 | `count' == 7 | `count' == 8 | `count' == 9 | `count' == 10  {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 
stats_table_a `y'
stats_table_pvalue `y'

}

		local count = `count' + 1
		
}



esttab ols1 ols2 ols3 ols4 ols5 ols6 ols7 ols8 ols9 ols10 using "$tables/event1_het_agg_MRCOD_`n'.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

eststo clear
		
restore
		
}
}

global horse "D_pscore D_pop05_total D_hh05_piped D_hh05_sewer D_geo_pop_dens05 D_dis_educ_secomp05 D_dis_elecp05 D_renamu_pia05 D_renamu_int05 D_renamu_atform05 D_renamu_dhealth05"
global het "D_pscore"
	
	
*IMR
*------------	
local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D $horse , a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/HET_horse_IMR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D D_pscore) ///
stats(Cmean, fmt(%9.2f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"))	

est clear

*U5MR
*------------	
local count = 1
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other  {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D $horse , a(i t) cluster(i)
estimates store m`count'
stats_table_a `y' 
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/HET_horse_U5MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D D_pscore) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

est clear



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure F5: Dynamic effect on open projects
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


drop *event*


cap drop D_pscore
foreach het of varlist pscore { 
	cap drop `het'm 
	cap drop *event_`het'
	
	sum `het' if year ==2015, det
	gen `het'm=`het'>=`r(p50)' if `het'!=.
	gen D_`het'=D*`het'm
	lab var D_`het' "Implementation $\times$ High intensity"


lab var D "Implementation"

}


forvalues l = 0/10 {
	gen L`l'event = K==`l'
}
forvalues l = 1/10 {
	gen F`l'event = K==-`l'
}


rename pscore geo_suitable

sum geo_suitable if year==2015, det
gen pscore=geo_suitable>=`r(p50)'

*By suitability
forvalues l = 0/10 {
	gen L`l'event_s1 = L`l'event*s1
	gen L`l'event_pscore = L`l'event*pscore
}
forvalues l = 1/10 {
	gen F`l'event_s1= F`l'event*s1
	gen F`l'event_pscore = F`l'event*pscore
}

gen pscorey=pscore*year


drop F7event
drop F7event_pscore


qui{

foreach y of varlist s1 { //

reghdfe `y' F*event_pscore F*event L*event_pscore L*event, a(i t) cluster(i) // 
testparm F*event_pscore

event_plot, stub_lag(L#event_pscore) stub_lead(F#event_pscore)  plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation x High intensity") ytitle("Open projects") title("") $xaxis_start legend(order(1 "Pre-trend" 3 "Implementation"))) ///
 trimlead(6) trimlag(7) together ///		
		lag_opt(color(navy) msymbol(S)) ///
		lead_opt(color(maroon)) ///
		lag_ci_opt(color(navy%45 navy%45) lwidth(thick)) ///
		lead_ci_opt(color(maroon%45 maroon%45)) 
		
graph export "$graphs/event1_geo_`y'.pdf", replace

est clear

}
}
	


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table F5: Effect of open projects on early-life mortality, 2SLS
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

xtset i t

lab var s1 "Open projects"


local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other  { 
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

xtivreg2 `y' year_* (s1 = D D_pscore) , fe cluster(i) first 
estimates store ols`count' 
stats_table_a `y'
estadd scalar AR = e(arfp)


	}
	
	
local count = `count' + 1

}

esttab ols1 ols2 ols3 ols4 ols5  using "$tables/IV_IMR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(s1) ///
stats(AR Cmean, fmt(%9.2f %9.2f) ///
labels("[1ex] AR \textit{p}-values" "\\[-0.7em] Mean (pre-start)"))

eststo clear




	
local count = 1
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other  { 
    
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

xtivreg2 `y'  year_* (s1 = D D_pscore) , fe cluster(i) first 
estimates store ols`count' 
stats_table_a `y'
estadd scalar AR = e(arfp)

	}
	
	
local count = `count' + 1

}

esttab ols1 ols2 ols3 ols4 ols5  using "$tables/IV_U5MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(s1) ///
stats(AR Cmean N Nc, fmt(%9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] AR \textit{p}-values" "\\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))

eststo clear





	
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% G Additional material for Section IV.4.ii, Completion
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table G1: Treatment status and timing of completion
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear


//Globals				
global mr "vs_imr1y vs_u5mr vs_imr1y_chg vs_u5mr_chg" 
global mr_prechgm "vs_imr1y_prechgm vs_u5mr_prechgm" 
global censo "inei_pop geo_pop_dens05 dis_piped dis_hh_sewer dis_hh_onsite dis_hh_od dis_educ_secp dis_elecp"
global muni "renamu_pia renamu_int renamu_atform renamu_dhealth" 
global muni_prechgm "inei_pop_prechgm geo_pop_dens_prechgm renamu_pia_prechgm renamu_int_prechgm renamu_atform_prechgm renamu_dhealth_prechgm"

global pol "pct_reelection_1993"
global pol_prechgm "turnover_prechgm"

global mr "vs_imr1y vs_u5mr"

//Initial (2005)
lab var vs_imr1y_chg "$\triangle$ IMR (2005-2015)"
lab var vs_u5mr_chg "$\triangle$ U5MR (2005-2015)"

lab var vs_imr1y "\textit{1. Outcomes} \\ IMR (per 1,000 infants)"			
lab var pop_u5y_1y "Infants"
lab var vs_u5mr "U5MR (per 1,000 children)"	
lab var pop_u5y	"Under-5 population"	

lab var inei_pop "\textit{2. District characteristics} \\ Population"
lab var dis_piped "Share HH piped water"
lab var geo_pop_dens "Population density (pop/km$^2$)"

lab var renamu_pia "\textit{3. Municipal characteristics} \\ Municipal budget (2010 USD, million)"
lab var renamu_int "Internet connectivity"
lab var renamu_atform "Technical support"

lab var geo_slope2_perc "\textit{1. Geographical features} \\ Share district gradient (0.8, 4.19]\%"
lab var geo_slope3_perc "Share district gradient (4.19, 13]\%"
lab var geo_slope4_perc "Share district gradient above 13\%"

lab var geo_elev_251500perc "Share district elevation (250, 500] mamsl"
lab var geo_elev_5011000perc "Share district elevation (500, 1000] mamsl"
lab var geo_elev_1001perc "Share district elevation above 1000 mamsl"

lab var geo_area "District area (km$^2$)"
lab var river_leng "River density ($\frac{km}{km^2}$)"

//Pre-start change
lab var vs_imr1y_prechgm "\textit{1. Outcomes} \\ $\triangle$ IMR"
lab var vs_u5mr_prechgm "$\triangle$ U5MR"

lab var inei_pop_prechgm "\textit{2. District characteristics} \\ $\triangle$ Population"
lab var geo_pop_dens_prechgm " $\triangle$ Population density"

lab var renamu_pia_prechgm "\textit{3. Municipal characteristics} \\ $\triangle$ Municipal budget"
lab var renamu_int_prechgm "$\triangle$ Internet connectivity"
lab var renamu_atform_prechgm "$\triangle$ Technical support"
lab var renamu_dhealth_prechgm "$\triangle$ Manages health centers"
lab var turnover_prechgm "$\triangle$ Political turnover"



*T STATUS AND COX HAZARD MODEL
*----------------------------------------

global miss "pop_total_miss geo_pop_dens05_miss dis_piped_miss dis_hh_sewer_miss dis_hh_onsite_miss dis_hh_od_miss dis_educ_secp_miss dis_elecp_miss renamu_pia_miss renamu_int_miss renamu_atform_miss renamu_dhealth_miss"

global xlist_geo "geo_slope2_perc geo_slope3_perc geo_slope4_perc geo_elev_251500perc geo_elev_5011000perc geo_elev_1001perc geo_area river_leng geo_region1 geo_region2"


preserve

reg treat_y2 $xlist_geo $censo $muni if year==2005 & treat_y0==1, robust 
estimates store m1

reg treat_y2 $xlist_geo $censo $muni $pol if year==2005 & treat_y0==1, robust 
estimates store m2


*COX-HAZARD MODEL: SETTING UP DATA
*------------------------------------

global time spell
global event event

keep if s0_year!=.

//Only for 2005: 
foreach var of varlist $censo $muni $pol {
	replace `var'=. if year!=2005
}


//Time
gen spell=1 if year<=s2_year

//Event 
gen event=s2_year==year 

 foreach v of var * {
 	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
  	}
  }

collapse (sum) spell (max) event (mean) $xlist_geo $censo $muni $pol , by(ubigeo_id)


foreach v of var * {
 	label var `v' `"`l`v''"'
  }


//Shape data as survival time
stset $time, failure($event)
stdescribe
stsum

*Non-parametric estimation
**Graph of hazard ration
sts graph, title("") graphregion(color(white)) xtitle("Years until completion") xlabel(0(2)10) title("Hazard: completion of implementation phase")
graph export "$graphs/hazard_complete.pdf", replace

*Parametric model (Cox)
stcox $xlist_geo $censo $muni, nohr
estimates store m3

stcox $xlist_geo $censo $muni $pol, nohr
estimates store m4

restore

esttab m1 m2 m3 m4 using "$tables/hazard_model_complete.tex", replace b(2) se(2) star f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses keep($xlist_geo $censo $muni $pol) label collabels(none) 

est clear



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure G2: Effect of infrastructure development, excluding districts that completed works
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear

//Drop districts that finished the implementation phase
gen s2_ever=s2_year!=.
drop  if s2_ever==1


qui{
foreach y of varlist vs_imr1y  { 

reghdfe `y' F*event L*event, a(i t) cluster(i) 
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr  legend(off) name(gimr1, replace)) ///
together trimlead(6) trimlag(7) ///		
		lag_opt(color(navy) msymbol(S)) ///
		lag_ci_opt(color(navy%60 navy%60) lwidth(thick))  alpha(0.1)

		
		
		graph export "$graphs/event1_`y'_s2ever.pdf", replace

est clear

}

foreach y of varlist vs_u5mr {

reghdfe `y' F*event L*event, a(i t) cluster(i) 
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr  legend(off)  name(gu5mr1, replace)) /// 
together trimlead(6) trimlag(7) ///		
		lag_opt(color(navy) msymbol(S)) ///
		lag_ci_opt(color(navy%60 navy%60) lwidth(thick))  alpha(0.1)
		
		graph export "$graphs/event1_`y'_s2ever.pdf", replace

est clear

}
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure G1: Effect of completion on early-life mortality
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

//Keep only treated districts
keep if s0_year!=.

//Keep years after started
keep if year>=s0_year

cap drop i t Ei K D 
cap drop L*event F*event lastcohort

gen i = ubigeo_id	// unit id
gen t = year		// calendar period
tsset i t

gen Ei = s2_year	// year when unit finishes being treated (last project to be completed)
bys i (t): replace Ei = Ei[1]
gen K = t-Ei 			// "relative time", i.e. the number periods since treated (could be missing if never-treated or never finished being treated)
gen D = K>=0 & Ei!=. 	//treat_post, AB:=0 even if never treated


sum Ei
gen lastcohort = Ei==r(max) // dummy for the latest- or never-treated cohort

//Get leads and lags for all districts (0 always if never experienced completion)
forvalues l = 0/10 {
	gen L`l'event = K==`l'
}
forvalues l = 1/10 {
	gen F`l'event = K==-`l'
}

drop F1event // normalize K=0 (and also K=-15) to zero	
gen F1event=0


qui{
    
foreach y of varlist vs_imr1y { 

reghdfe `y' F*event L*event, a(i t) cluster(i)
event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///default_look
xtitle("Years since completion") ytitle("Average causal effect") title("") $xaxis_comp $yaxis_imr_comp legend(off)) ///
	trimlag(4) trimlead(6) together	///
	lag_opt(color(forest_green)) ///
	lag_ci_opt(color(forest_green%60 forest_green%60) lwidth(thick)) alpha(0.1)

graph export "$graphs/event2_`y'.pdf", replace

}

foreach y of varlist vs_u5mr { 

reghdfe `y' F*event L*event, a(i t) cluster(i)

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///default_look
xtitle("Years since completion") ytitle("Average causal effect") title("") $xaxis_comp $yaxis_u5mr_comp legend(off)) ///
		 trimlag(4)  trimlead(6) together ///
	lag_opt(color(forest_green)) ///
	lag_ci_opt(color(forest_green%60 forest_green%60) lwidth(thick)) alpha(0.1)

graph export "$graphs/event2_`y'.pdf", replace

}
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Table G2: Static effect of infrastructure completion on early-life mortality
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label var D "Completion"

qui{
	
*IMR
*------------	
local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D, a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD2_IMR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D) ///
stats(Cmean, fmt(%9.2f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"))	

est clear

*U5MR
*------------	
local count = 1
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other  {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D, a(i t) cluster(i)
estimates store m`count'
stats_table_a `y' 
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD2_U5MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

est clear

}


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure G3: Effect of infrastructure development, setting as control in-between periods
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear
cap drop s1_stop*
sort ubigeo_id year
bysort ubigeo_id: gen s1_stop=1 if s1[_n]==0 & s1[_n-1]>0 & year!=2005 
bysort ubigeo_id: gen s1_stop_nos2=1 if s1[_n]==0 & s1[_n-1]>0 & year!=2005 & s0_max>s0 
bysort ubigeo_id: egen s1_stop_nos2_max=max(s1_stop_nos2)

bysort ubigeo_id: gen s1_stop_nos2_year=year if s1_stop_nos2==1
bysort ubigeo_id: egen s1_stop_nos2_maxyear=max(s1_stop_nos2_year)
cap drop s1_stop_nos2_dur
bysort ubigeo_id: gen s1_stop_nos2_dur=1 if year>=s1_stop_nos2_maxyear & s1==0

preserve
collapse (sum) s1_stop_nos2_dur, by(ubigeo_id)
sum s1_stop_nos2_dur if s1_stop_nos2_dur>0 //74 obs - 2.6 years
restore


* EVENT PLOT - ALL MORTALITY - IN BETWEEN PERIODS AS CONTROL
foreach var of varlist F*event L*event{
	replace `var'=0 if s1_stop_nos2_dur==1
}


qui {

foreach y of varlist vs_imr1y  { 
	

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr  legend(off) name(gimr1, replace)) /// 
together trimlead(6) trimlag(7) ///		
		lag_opt(color(navy) msymbol(S)) ///
		lag_ci_opt(color(navy%60 navy%60) lwidth(thick))  alpha(0.1)


graph export "$graphs/event1_`y'_revert.pdf", replace

est clear

}


foreach y of varlist vs_u5mr {

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr  legend(off)  name(gu5mr1, replace)) /// 
together trimlead(6) trimlag(7) ///		
		lag_opt(color(navy) msymbol(S)) ///
		lag_ci_opt(color(navy%60 navy%60) lwidth(thick))  alpha(0.1)

graph export "$graphs/event1_`y'_revert.pdf", replace

est clear

}
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% H Additional material for Section V1, Policy implications
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure H2: Duration and halting
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear


*Duration by halting status
*-------------------------------
drop st
gen s1_d=1 if s1>0 & s1!=.
cap drop st
bysort ubigeo_id: egen st=total(s1_d)

preserve
collapse (max) st (min) s0_year s2_year (max) p_aband_max, by(ubigeo_id)

drop if s0_year==.

sum st, det
sum st if p_aband_max==0, det
sum st if p_aband_max==1, det

twoway histogram st if p_aband_max==0, discrete percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7) || ///
histogram st if p_aband_max>0 & p_aband_max!=., discrete percent graphregion(color(white)) bfcolor(none) blcolor(black) title("") xtitle("") ytitle("{bf: % Districts}", size(medium)) xlabel(1(1)11) ylab(, nogrid)  ///
xtitle("Duration of implementation phase in years") legend(order(1 "No projects halted" 2 "Projects halted")) xline(2.6, lp(dash) lcolor(maroon)) xline(5.3, lp(dash_dot) lcolor(navy))
graph export "$graphs/dist_duration_halted.pdf", replace
restore

*Share projects are halted in a district
*-----------------------------------------------

preserve
keep if year==2015
keep if s0_year!=.
twoway histogram  p_aband_max, percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7)  ytitle("{bf: % Districts}", size(medium))
graph export "$graphs/dist_p_aband_max.pdf", replace

restore


*Years projects are halted in a district
*-----------------------------------------------
preserve
sort ubigeo year
gen p_aband_d=1 if p_aband_num!=0
bysort ubigeo: egen p_aband_t=sum(p_aband_d)

keep if year==2015
keep if s0_year!=.

twoway histogram  p_aband_t, discrete percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7)  ytitle("{bf: % Districts}", size(medium)) xtitle("Years at least one project halted") xline(3.6, lp(dash) lcolor(maroon))
graph export "$graphs/dist_p_aband_t.pdf", replace

sum p_aband_t

restore


*DESCRIPTIVES: HALTING - PROJECT LEVEL
*----------------------------------------------

loaddata_proj

/*share abandoned by 2015

. unique ubigeo_id snip
Number of unique values of ubigeo_id snip is  6099
Number of records is  67089

. unique ubigeo_id snip if p_aband_2015==1
Number of unique values of ubigeo_id snip is  2444
Number of records is  2444

. dis 2444/6099
.40072143
*/



preserve
collapse (sum) p_sewerage_y1 (max) p_sewerage_y2 p_sewerage_y0y_max p_aband_max , by(ubigeo snip)
rename p_sewerage_y1 st

*Duration
tw histogram st if p_sewerage_y2==1, percent discrete bfcolor(navy*0.7) blcolor(navy*0.7)  || histogram st if p_sewerage_y2==0 , percent discrete graphregion(color(white)) bfcolor(none) blcolor(black) title("") ytitle("{bf: % Projects}", size(medium) ) ylab(, nogrid)  xtitle("Duration of project in years") legend(order(1 "Completed" 2 "Not completed")) xlabel(0(1)11) //
graph export "$graphs/p_duration.pdf", replace


*Duration by halting
twoway histogram st if p_aband_max==0, discrete percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7) || ///
histogram st if p_aband_max>0 & p_aband_max!=., discrete percent graphregion(color(white)) bfcolor(none) blcolor(black) title("") xtitle("") ytitle("{bf: % Projects}", size(medium)) xlabel(1(1)11) ylab(, nogrid)   ///
xtitle("Duration of project in years") legend(order(1 "Never halted" 2 "Ever halted")) xline(2.1, lp(dash) lcolor(maroon)) xline(4.4, lp(dash_dot) lcolor(navy))
graph export "$graphs/p_duration_halted.pdf", replace

sum st if p_aband_max==0
sum st if p_aband_max>0 & p_aband_max!=.


*Duration by halting, for completed projects
twoway histogram st if p_aband_max==0 & p_sewerage_y2==1, discrete percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7) || ///
histogram st if p_aband_max==1 & p_sewerage_y2==1, discrete percent graphregion(color(white)) bfcolor(none) blcolor(black) title("") xtitle("") ytitle("{bf: % Projects}", size(medium)) xlabel(1(1)11) ylab(, nogrid) ///
xtitle("Duration of project in years") legend(order(1 "Never halted" 2 "Ever halted"))
graph export "$graphs/p_duration_halted_comp.pdf", replace
restore



*Duration of halting
*-----------------------------------------------------------------
preserve
keep if p_sewerage_y0y_max>=2005 & p_sewerage_y0y_max<=2013

collapse (max) p_cum_perc p_sewerage_y0y p_sewerage_y2 p_aband p_aband_tot  (mean) p_sewerage_t p_popbenef_m, by(snip)

recode p_aband (.=0)

tw histogram p_aband_tot if p_cum_perc>0 & p_sewerage_y0y==2013, discrete percent graphregion(color(white)) bfcolor(navy*0.7) blcolor(navy*0.7) || ///
histogram p_aband_tot if p_cum_perc>0  & p_sewerage_y0y<2013, discrete percent graphregion(color(white)) bfcolor(none) ///
blcolor(black) title("") ytitle("{bf: % Projects}", size(medium) ) xlabel(0(1)10, valuelabel) ylab(, nogrid) ///
xtitle("Years project is halted") legend(order(2 "2005-2012" 1 "2013")) xline(2.5, lp(dash) lcolor(maroon)) 
graph export "$graphs/p_aband_tot_y0.pdf", replace

sum p_aband_tot if p_sewerage_y0y==2013 //1 year
sum p_aband_tot if p_sewerage_y0y>=2005 & p_sewerage_y0y<=2012 //2.87 years
sum p_aband_tot //2.5 years

restore



*Share halted by calendar year
*-----------------------------------------------------------------

preserve

keep if p_sewerage_y0y_max>=2005 & p_sewerage_y0y_max<=2015

sort ubigeo snip year

replace p_aband=0 if p_sewerage_y2==1

tab year, sum(p_aband)

collapse (mean) p_aband, by(year)

tw bar p_aband year, graphregion(color(white)) bfcolor(navy*0.4) blcolor(navy*0.4) ///
blcolor(black) title("") ytitle("{bf: Share of projects halted (out of started)}", size(medium) ) xlabel(2006(1)2015, valuelabel) ylab(, nogrid) ///
xtitle("")
graph export "$graphs/p_aband_share.pdf", replace

restore



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure H1: Pair-wise correlation matrix across project characteristics
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


preserve

tab p_type_cat, gen(p_type_cat)

replace p_formu=1 if p_formu<=3
tab p_formu, gen(p_formu)

bysort ubigeo year: egen dis_sewerage_y0=sum(p_sewerage_y0)

collapse (max) p_sewerage_ts0 p_sewerage_t p_sewerage_y2 p_aband_max p_type_cat1 p_type_cat2 p_type_cat3 pscore costoactual p_popbenef p_overrunp p_formu1 p_formu2, by(ubigeo snip)

lab var p_sewerage_ts0 "Years until start"
lab var p_sewerage_t  "Duration"
lab var p_sewerage_y2 "Completed"
lab var p_aband_max 	"Halted"
lab var p_type_cat1 	"Construction"
lab var p_type_cat2 "Expansion"
lab var p_type_cat3 "Improvement"
lab var p_formu1 "Imp agency: local"
lab var p_formu2 "Imp agency: central"
lab var costoactual "Investment amount"
lab var p_overrunp "Cost overrun (%)"
lab var p_popbenef "Targeted pop"
lab var pscore 	"Geographic suitability"

			pwcorr 	p_sewerage_ts0 p_sewerage_t p_sewerage_y2 p_aband_max p_overrunp p_type_cat1 p_type_cat2 p_type_cat3 p_formu1 p_formu2 costoactual p_popbenef   
			matrix C = r(C)

			heatplot C, values(format(%3.1f) mlabsize(tiny)) ///
			cuts(-0.30(0.05)0.30) colors(mono) keylabels(minmax, interval) legend(off) aspectratio(1) lower nodiagonal ///
			ylabel(, nogrid labsize(small)) xlabel(, angle(45) nogrid labsize(small)) label graphregion(color(white) margin(zero))
			
			graph export "$graphs/pwcorr_matrix.pdf", replace

restore


		
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure H4: Effect of halting infrastructure on early-life mortality (per 1,000)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

//Year when first project halted
gen s0_year_abandx=year if p_aband_num>0
bysort ubigeo: egen s0_year_aband=min(s0_year_abandx)
drop s0_year_abandx

//Sample: years and districts after start
keep if s0_year!=.
keep if year>=s0_year

cap drop i t Ei K D lastcohort *event*

gen i = ubigeo_id	// unit id
gen t = year		// calendar period
tsset i t

replace s0_year_aband=s0_year_aband+1
gen Ei = s0_year_aband	// year when unit is first treated
bys i (t): replace Ei = Ei[1]
gen K = t-Ei 			// "relative time", i.e. the number periods since treated (could be missing if never-treated)
gen D = K>=0 & Ei!=. 	//treat_post, AB:=0 even if never treated

lab var D "Post"

//Get leads and lags
sum Ei
gen lastcohort = Ei==r(max) // dummy for the latest- or never-treated cohort
forvalues l = 0/10 {
	gen L`l'event = K==`l'

}
forvalues l = 1/10 {
	gen F`l'event = K==-`l'
}

drop F1event // normalize K=-1 to zero	
gen F1event=0


qui{
foreach y of varlist vs_imr1y  { 

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///default_look
xtitle("") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr legend(off))  ///
 trimlead(6) trimlag(7) together ///		
		lag_opt(color(cranberry) msymbol(S)) ///
		lag_ci_opt(color(cranberry%60 cranberry%60) lwidth(thick))  alpha(0.1)

		

graph export "$graphs/event4_`y'.pdf", replace

est clear

}

foreach y of varlist vs_u5mr {

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

event_plot, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap)  ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///default_look
xtitle("") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr legend(off)) ///
 trimlead(6) trimlag(7) together ///		
		lag_opt(color(cranberry) msymbol(S)) ///
		lag_ci_opt(color(cranberry%60 cranberry%60) lwidth(thick))  alpha(0.1)

graph export "$graphs/event4_`y'.pdf", replace

est clear

}
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*Figure H3: Correlation between duration and halting, district level
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean", clear

cap drop s1miss
gen s1miss=s1
replace s1miss=. if s1==0

cap drop duration
bysort ubigeo: egen duration=count(s1miss)

		preserve
		egen byte d = cut(pscore), group(100)
		set type double
		
		collapse (max) pscore = pscore s0=s0 d duration (mean) s1=s1 p_aband = p_aband , by(ubigeo)
		
		collapse (mean) pscore = pscore duration = duration p_aband = p_aband s0=s0 s1=s1, by(d)
		
		*Duration - halted
		keep if s0!=0
twoway scatter s0 duration  || lfit s0 duration, lcolor(navy) || scatter p_aband duration, yaxis(2)  mcolor(cranberry) || lfit p_aband duration,  yaxis(2) lcolor(cranberry) ///
		, graphregion(color(white)) xlabel(1(1)10) xtitle("Duration of implementation phase (years)") legend(order(2 "Started projects" 4 "Halted/open projects"))
		
		graph export "$graphs/duration_halted.pdf", replace	
		
		restore


